#pragma once
#include "const.h"
#include "configMgr.h"

class RedisConPool {
public:
	RedisConPool(size_t poolsize, const char* host, int port,const char* pwd);
	~RedisConPool();
	redisContext* getContext();
	void returnContext(redisContext*);
	void Close();
private:
	std::mutex _mtx;
	std::atomic<bool> _b_stop;
	std::condition_variable _condvar;
	size_t _poolsize;
	const char* _host;
	int _port;
	const char* _pwd;
	std::queue<redisContext*> _cons;

};

class RedisMgr:public std::enable_shared_from_this<RedisMgr>
{
public:
	~RedisMgr();
	static RedisMgr& GetInstance();
	bool Get(const std::string& key, std::string& value);
	bool Set(const std::string& key, const std::string& value);
	bool Auth(const std::string& password);
	bool LPush(const std::string& key, const std::string& value);
	bool LPop(const std::string& key, std::string& value);
	bool RPush(const std::string& key, const std::string& value);
	bool RPop(const std::string& key, std::string& value);
	bool HSet(const std::string& key, const std::string& hkey, const std::string& value);
	bool HSet(const char* key, const char* hkey, const char* hvalue, size_t hvaluelen);
	bool HDel(const std::string& key, const std::string& field);
	std::string HGet(const std::string& key, const std::string& hkey);
	bool Del(const std::string& key);
	bool ExistsKey(const std::string& key);
	void Close();
private:
	//redisContext* _connect;
	//redisReply* _reply;

	std::unique_ptr<RedisConPool> _con_pool;

	RedisMgr();
};

